import { JSBridge } from "../JSBridge";
import ShareBase from "../ShareBase";
import { TouTiaoConfig } from "./TouTiaoConfig";
import QuLe from "../QuLe";

export enum TouTiaoSharePos { Top, Bottom }

export default class TouTiaoShare extends ShareBase {

    private readonly successAfterSeconds = 3;

    init(title: string, query: string = '') {
        super.init(title, query);
        let tt = JSBridge.getTTObj();
        tt.showShareMenu({ withShareTicket: true });
        tt.onShareAppMessage((res) => {
            return {
                templateId: QuLe.instance.remoteConfig.toutiaoShareTemplateId,
                query: "a=1&b=2",
                success() {
                    console.log("头条分享成功");
                },
                fail() {
                    console.log("头条分享失败");
                }
            }
        });
    }

    /**
     * 分享审核通过的模板
     * @param query "nick=aaa&gender=0"
     * @param onShareFinish onShareFinish(success:boolean)
     */
    share(title: string, query: string = '', onShareFinish?: Function) {
        super.share(title, query, onShareFinish);
        let tt = JSBridge.getTTObj();
        let _this = this;
        tt.shareAppMessage({
            templateId: QuLe.instance.remoteConfig.toutiaoShareTemplateId,
            query: query,
            success() {
                console.log("头条分享成功,继续用时间判断");
                if (_this.onShareFinish) {
                    if (new Date().getTime() - _this.shareStartTime > 1000 * _this.successAfterSeconds) {
                        _this.onShareFinish(true);
                    } else {
                        _this.onShareFinish(false);
                    }
                }
            },
            fail(err) {
                console.log("头条分享失败：" + JSON.stringify(err));
                if (_this.onShareFinish) {
                    _this.onShareFinish(false);
                }
            }
        });
    }

}
